<template>
  <div class="app-container">
    <!-- 视频管理-管理按钮 -->
    <div class="search-bar">
      <el-input v-model="searchData.spmc" placeholder="请输入视频名称查询" />
      <el-select v-model="searchData.zwpz" placeholder="请选择作物品种">
        <el-option label="全部" value=""></el-option>
        <el-option
          v-for="item in dict.dict.zzzw"
          :key="item.value"
          :value="item.value"
          :label="item.label"
        ></el-option>
      </el-select>
      <el-select v-model="searchData.szjd" placeholder="请选择生长阶段">
        <el-option label="全部" value=""></el-option>
        <el-option
          v-for="item in dict.dict.szjd"
          :key="item.value"
          :value="item.value"
          :label="item.label"
        ></el-option>
      </el-select>
      <el-button
        v-waves
        type="primary"
        size="small"
        icon="el-icon-search"
        @click="searchBtnHandle"
        >查询</el-button
      >
      <el-button
        v-waves
        type="info"
        size="small"
        icon="el-icon-refresh"
        @click="resetTableList"
        >显示全部</el-button
      >
      <div style="float: right">
        <el-button
          v-waves
          type="primary"
          icon="el-icon-plus"
          @click="openAdd"
          size="small"
          v-permission="'jtnc-spgl-krSpgl-add'"
          >上传新视频
        </el-button>
      </div>
    </div>
    <!-- 视频管理-列表 -->
    <el-table
      ref="dataTable"
      :data="tableData"
      stripe
      border
      @selection-change="handleTableSelectChange"
      v-loading="isLoading"
    >
      <el-table-column
        type="selection"
        width="50"
        align="center"
        header-align="center"
      />
      <el-table-column label="视频编号" prop="spbh" align="center" />
      <el-table-column label="视频名称" prop="spmc" align="center" />
      <el-table-column label="作物品种" prop="zwpz" align="center" />
      <el-table-column label="生长阶段" prop="szjd" align="center" />
      <el-table-column label="视频简介" prop="spjj" align="center" />
      <el-table-column label="上传时间" prop="createTime" align="center" />
      <el-table-column fixed="right" label="操作" width="120" align="center">
        <template v-slot="scope">
          <el-button
            type="text"
            style="color: #13ce66"
            size="small"
            @click="openView(scope.row)"
            >详情</el-button
          >
          <el-button
            v-permission="'jtnc-spgl-krSpgl-update'"
            type="text"
            size="small"
            @click="openUpdate(scope.row)"
            >修改
          </el-button>
          <el-button
            v-permission="'jtnc-spgl-krSpgl-delete'"
            style="color: #ff6d6d"
            type="text"
            size="small"
            @click="deleteByIds(scope.row)"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 视频管理-分页 -->
    <el-pagination
      style="text-align: center; margin-top: 10px"
      layout="total,prev,pager,next,sizes,jumper"
      :page-size="pager.limit"
      :current-page="pager.page"
      :total="pager.totalCount"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <!-- 添加修改弹窗 -->
    <el-dialog
      :title="titleMap[dialogType]"
      :close-on-click-modal="dialogType !== 'view' ? false : true"
      :visible.sync="dialogFormVisible"
      @close="resetForm"
      width="800px"
      :key="'myDialog' + dialogIndex"
    >
      <el-form
        ref="dataForm"
        :model="temp"
        label-position="right"
        label-width="100px"
        :disabled="dialogType === 'view'"
      >
        <el-form-item
          label="视频编号"
          prop="spbh"
          :rules="[]"
          v-if="dialogType != 'add'"
        >
          <el-input
            v-model="temp.spbh"
            placeholder="请输入视频编号"
            :disabled="true"
          />
        </el-form-item>
        <el-form-item
          label="视频名称"
          prop="spmc"
          :rules="[
            { required: true, message: '请输入视频名称', trigger: 'blur' },
          ]"
        >
          <el-input v-model="temp.spmc" placeholder="请输入视频名称" />
        </el-form-item>
        <el-form-item
          label="作物品种"
          prop="zwpz"
          :rules="[
            { required: true, message: '请输入作物品种', trigger: 'blur' },
          ]"
        >
          <el-select v-model="temp.zwpz" placeholder="请选择作物品种">
            <el-option
              v-for="item in dict.dict.zzzw"
              :key="item.value"
              :value="item.value"
              :label="item.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="生长阶段"
          prop="szjd"
          :rules="[
            { required: true, message: '请输入生长阶段', trigger: 'blur' },
          ]"
        >
          <el-select v-model="temp.szjd" placeholder="请选择生长阶段">
            <el-option
              v-for="item in dict.dict.szjd"
              :key="item.value"
              :value="item.value"
              :label="item.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="视频简介"
          prop="spjj"
          :rules="[
            { required: true, message: '请输入视频简介', trigger: 'blur' },
          ]"
        >
          <el-input
            v-model="temp.spjj"
            type="textarea"
            maxlength="65535"
            placeholder="请输入视频简介"
          />
        </el-form-item>

        <el-form-item
          label="视频封面图"
          prop="picUrl"
          :rules="[
            { required: true, message: '请上传封面图', trigger: 'blur' },
          ]"
        >
          <image-one v-model="temp.picUrl" name="avatar" />
        </el-form-item>
        <el-form-item
          label="视频地址"
          prop="spdz"
          :rules="[{ required: true, message: '请上传视频', trigger: 'blur' }]"
        >
          <video-upload-chunk
            ref="videoUp"
            upload-server-url="/upload/chunks"
            upload-dir="testChunks"
            max-file-size="700mb"
            chunk-size="10mb"
            :videoUrl="temp.spdz"
            :mime-types="[
              { title: 'Video files', extensions: 'avi,mov,wmv,mp4' },
            ]"
            key="chunk"
          ></video-upload-chunk>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          v-waves
          type="primary"
          v-if="dialogType !== 'view'"
          @click="saveSpData"
          >保存</el-button
        >
        <el-button v-waves @click="dialogFormVisible = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import waves from "@/directive/waves";
import crudMixin from "@/mixin/crudMixin";
import VideoUploadChunk from "@/components/Upload/VideoUploadChunk.vue";
import ImageOne from "@/components/Upload/ImageOne";
export default {
  directives: { waves },
  mixins: [crudMixin],
  components: { VideoUploadChunk, ImageOne },
  dicts: ["zzzw", "szjd"],
  data() {
    return {
      url: {
        list: "/jtnc/spgl/krSpgl/list",
        add: "/jtnc/spgl/krSpgl/add",
        info: "/jtnc/spgl/krSpgl/getById",
        update: "/jtnc/spgl/krSpgl/update",
        delete: "/jtnc/spgl/krSpgl/delete",
        export: "/jtnc/spgl/krSpgl/export/excel",
      },
      fileInfo: "",
      idName: "spglId",
    };
  },
  created() {
    this.loadTableList();
    this.resetTemp();
  },
  methods: {
    saveSpData() {
      if (!this.$refs.videoUp.uploadData) {
        this.$message.error("请上传视频！");
        return false;
      }
      this.temp.spdz = this.$refs.videoUp.uploadData.fileUrl;
      this.saveData();
    },
    resetForm() {
      this.resetTemp();
      this.fileInfo = "";
    },
  },
};
</script>
<style lang="scss" scoped>
.search-bar {
  margin-bottom: 10px;
  ::v-deep {
    .el-input,
    .el-select {
      width: 180px;
      margin-right: 10px;
      margin-bottom: 5px;
    }
  }
}
</style>
